from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .Ser import *
# Create your views here.


class News_V(APIView):
    def get(self,request):
        id = request.GET.get("id")
        if id:
            data1=News_M.objects.get(pk=id)
            data1.count+=1
            data1.save()
            data=News_M.objects.get(pk=id)
            data_ser=News_SER(data)
            return Response(data_ser.data)
        else:
            data=News_M.objects.all()
            data_ser=News_SER(data,many=True)
            return Response(data_ser.data)



class User_V(APIView):
    def post(self,request):
        name = request.data.get("name")
        password = request.data.get("password")
        users = User_M.objects.filter(name=name).first()
        if users:
            if users.password == password:
                return Response({"code":200,"meg":"登录成功"})
            else:
                return Response({"code": 10011, "meg": "密码错误"})
        else:
            return Response({"code": 10010, "meg": "用户名不存在"})

